package frontend

import (
	"blog/service/page"
	"html/template"
	"net/http"
	"strconv"

	"gitee.com/xchengli/blog-lib/components"
	"gitee.com/xchengli/blog-lib/zlog"

	"github.com/gin-gonic/gin"
)

// 文章阅读页
func ReadHandle(ctx *gin.Context) {
	id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
	if err != nil {
		zlog.Error(ctx, err)
		ctx.JSON(http.StatusForbidden, gin.H{"error": "内容未找到"})
		return
	}

	// 使用PS层服务
	postPageService := page.NewPostPageService()
	info, err := postPageService.GetPostInfoForRead(ctx, id)
	if err != nil {
		ctx.JSON(http.StatusForbidden, gin.H{"error": "内容未找到"})
		return
	}

	// Access the embedded PostDataReal fields
	if info.IsTemp == 1 || info.IsShow == 0 || info.IsPrivate == 1 {
		ctx.JSON(http.StatusNotFound, gin.H{"error": "页面未找到"})
		return
	}

	param := struct {
		Info  interface{}
		QRUrl template.HTML
	}{
		Info:  info,
		QRUrl: template.HTML(components.GetCurrentUrl(ctx.Request) + "?fr=qrCode"),
	}

	ctx.HTML(http.StatusOK, "read.html", param)
}
